import 'package:flutter/material.dart';
import 'package:flutter_tech_shop/src/screens/profile_page.dart';
import 'package:flutter_tech_shop/src/shared/address.dart';
import 'package:get/get.dart';

class EditAddressPage extends StatelessWidget {
  const EditAddressPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('修改地址')),
      body: Obx(() =>
          ListView.separated(itemBuilder: (contetx, index) {
            Address addressItem = addressController.addresses[index];
            return Container(
              margin: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
              child: Row(
                children: [
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('名字: ${addressItem.name}'),
                        Text('电话: ${addressItem.phone}'),
                        Text('地址: ${addressItem.address}'),
                      ],
                    ),
                  ),
                  Obx(() => GestureDetector(
                    onTap: () {
                      addressController.currentAddress.value = index;
                    },
                    child: index == addressController.currentAddress.value ? Icon(
                        Icons.check_box) : Icon(
                        Icons.check_box_outline_blank_outlined),
                  ))
                ],
              ),
            );
          },
            shrinkWrap: true,
            itemCount: addressController.addresses.length,
            separatorBuilder: (BuildContext context, int index) {
              return Container(
                margin: EdgeInsets.symmetric(horizontal: 15),
                height: 1,
                color: Colors.grey,
              );
            },)),
    );
  }
}

class AddressController extends GetxController {
  final currentAddress = 0.obs;
  final addresses = <Address>[
    Address(
      name: 'xjn',
      address: '浙江省杭州市',
      phone: '13819145601',
    ),
    Address(
      name: '张三',
      address: '北京市朝阳区建国路88号SOHO现代城C座1201室',
      phone: '13819145601',
    ),
    Address(
      name: '李四',
      address: '上海市徐汇区虹桥路3号华亭宾馆705室',
      phone: '13819145601',
    ),Address(
      name: '王五',
      address: '广州市天河区体育西路101号101商铺',
      phone: '13819145601',
    ),
  ].obs;

  addAddress(Address address) {
    addresses.add(address);
  }
}
